package com.graphhopper.web.config;

import com.graphhopper.web.filter.CORSFilter;
import com.graphhopper.web.filter.HeadFilter;
import com.graphhopper.web.filter.IPFilter;
import com.graphhopper.web.servlet.*;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configurable
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean<HeadFilter> customHeadFilterRegistration() {
        FilterRegistrationBean<HeadFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new HeadFilter());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registration.addUrlPatterns("/**");
        return registration;
    }

    @Bean
    public FilterRegistrationBean<CORSFilter> customCorsFilterRegistration() {
        FilterRegistrationBean<CORSFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CORSFilter());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        registration.addUrlPatterns("/**");
        return registration;
    }

    //@Bean
    //public FilterRegistrationBean<IPFilter> customIPFilterRegistration() {
    //    FilterRegistrationBean<IPFilter> registration = new FilterRegistrationBean<>();
    //    registration.setFilter(new IPFilter());
    //    registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
    //    registration.addUrlPatterns("/**");
    //    return registration;
    //}

    @Bean
    public ServletRegistrationBean<I18NServlet> customI18NServletRegistration() {
        ServletRegistrationBean<I18NServlet> registration = new ServletRegistrationBean<>();
        registration.addUrlMappings("/i18n*");
        return registration;
    }

    @Bean
    public ServletRegistrationBean<InfoServlet> customInfoServletRegistration() {
        ServletRegistrationBean<InfoServlet> registration = new ServletRegistrationBean<>();
        registration.addUrlMappings("/info*");
        return registration;
    }

    @Bean
    public ServletRegistrationBean customGraphHopperServletRegistration() {
        ServletRegistrationBean<GraphHopperServlet> registration = new ServletRegistrationBean<>(new GraphHopperServlet(), "/route*");
        //registration.addUrlMappings("/route*");
        return registration;
    }

    @Bean
    public ServletRegistrationBean<NearestServlet> customNearestServletRegistration() {
        ServletRegistrationBean<NearestServlet> registration = new ServletRegistrationBean<>();
        registration.addUrlMappings("/nearest*");
        return registration;
    }

    //在这个位置还差了一个如果change_graph的话(修改graph)，执行的servlet，暂时先不需要

    @Bean
    public ServletRegistrationBean<InvalidRequestServlet> customInvalidRequestServletRegistration() {
        ServletRegistrationBean<InvalidRequestServlet> registration = new ServletRegistrationBean<>();
        registration.addUrlMappings("/**");
        return registration;
    }

}
